/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package models;

import com.sun.org.apache.xml.internal.serialize.IndentPrinter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author dybvik
 */
public class User {
    private int userID;
    private String userName;
    private String firstName;
    private String lastName;
    private String email;
    private String password;
    private Date dateRegistered;
    private Date dateLastLoggedIn;
    private boolean editable = false;
    private boolean newPasswordFlag = false;
    private boolean deleteFlag = false;
    private List<String> roles = new ArrayList<String>();
    private List<Department> departments = new ArrayList<Department>();

    public User() {
    }
    
    /**
     * Komplett konstruktør
     * @param userID
     * @param userName
     * @param firstName
     * @param lastName
     * @param email
     * @param password
     * @param dateRegistered
     * @param dateLastLoggedIn
     * @param roles
     */
    public User(int userID, String userName, String firstName, String lastName, String email, String password, Date dateRegistered, Date dateLastLoggedIn, List<String> roles) {
        this.userID = userID;
        this.userName = userName;
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
        this.password = password;
        this.dateRegistered = dateRegistered;
        this.dateLastLoggedIn = dateLastLoggedIn;
        //roles.clear();
        this.roles = roles;
    }
    /**
     * Konstruktør uten roller som param
     * @param userID
     * @param userName
     * @param firstName
     * @param lastName
     * @param email
     * @param password
     * @param dateRegistered
     * @param dateLastLoggedIn
     */
    public User(int userID, String userName, String firstName, String lastName, String email, String password, Date dateRegistered, Date dateLastLoggedIn) {
        this.userID = userID;
        this.userName = userName;
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
        this.password = password;
        this.dateRegistered = dateRegistered;
        this.dateLastLoggedIn = dateLastLoggedIn;
    }


    // <editor-fold defaultstate="collapsed" desc="Getters and setters methods. Click on the + sign to see the methods.">
    public Date getDateLastLoggedIn() {
        return dateLastLoggedIn;
    }

    public void setDateLastLoggedIn(Date dateLastLoggedIn) {
        this.dateLastLoggedIn = dateLastLoggedIn;
    }

    public Date getDateRegistered() {
        return dateRegistered;
    }

    public void setDateRegistered(Date dateRegistered) {
        this.dateRegistered = dateRegistered;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
        //System.out.println(email);
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
        //System.out.println(firstName);
    }

    public String getLastName() {
        return lastName;
    }

    public List<Department> getDepartments() {
        return departments;
    }

    public void setDepartments(List<Department> departments) {
        this.departments = departments;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
        //System.out.println(lastName);
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
        //System.out.println(password);
    }

    public List<String> getRoles() {
        return roles;
    }

    public void setRoles(List<String> roles) {
        this.roles = roles;
        //System.out.println(roles.toString());
    }

    public void setRole(String role){
        int flag = -1;
        for(int i=0;i<roles.size();i++){
            if(roles.get(i).equalsIgnoreCase(role)) flag = i;
        }
        if(flag>=0) roles.remove(flag);
        roles.add(role);
        //System.out.println(role);
    }

    public int getUserID() {
        return userID;
    }

    public void setUserID(int userID) {
        this.userID = userID;
        //System.out.println(userID);
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
        //System.out.println(userName);
    }

    public boolean isEditable() {
        return editable;
    }

    public void setEditable(boolean editable) {
        this.editable = editable;
    }

    public boolean isSysadmin(){
        return checkRoleInRoles("sysadmin");
    }

    public boolean isAdminresource(){
       return checkRoleInRoles("adminresource");
    }

    public boolean isTeacher(){
        return checkRoleInRoles("teacher");
    }

    public boolean isStudyadmin(){
        return checkRoleInRoles("studyadmin");
    }

    public boolean isDeleteFlag() {
        return deleteFlag;
    }

    public void setSysadmin(boolean b){
        if(b) setRole("sysadmin");
        else removeRole("sysadmin");
    }

    public void setAdminresource(boolean b){
        if(b) setRole("adminresource");
        else removeRole("adminresource");
    }

    public void setTeacher(boolean b){
        if(b) setRole("teacher");
        else removeRole("adminresource");
    }

    public void setStudyadmin(boolean b){
        if(b) setRole("studyadmin");
        else removeRole("studyadmin");
    }

    private void removeRole(String role){
        int flag = -1;
        for(int i=0;i<roles.size();i++){
            if(roles.get(i).equalsIgnoreCase(role)) flag = i;
        }
        if(flag>=0) roles.remove(flag);
    }

    public void setDeleteFlag(boolean deleteFlag) {
        this.deleteFlag = deleteFlag;
    }

    public boolean isNewPasswordFlag() {
        return newPasswordFlag;
    }

    public void setNewPasswordFlag(boolean newPasswordFlag) {
        this.newPasswordFlag = newPasswordFlag;
    }

    
    

    // </editor-fold>

    private boolean checkRoleInRoles(String role){
        boolean b = false;
        for(String s: roles){
            if(s.equalsIgnoreCase(role))
                b = true;
        }
        return b;
    }

    @Override
    public String toString() {
        return "User{" + "userID=" + userID + "userName=" + userName + "firstName=" + firstName + "lastName=" + lastName + "email=" + email + "password=" + password + "dateRegistered=" + dateRegistered + "dateLastLoggedIn=" + dateLastLoggedIn + "roles=" + roles + '}';
    }
}
